<template>
  <div class="random">
    <div class="container">
      <div class="box1 box">小明</div>
      <div class="box2 box">小红</div>
      <div class="box3 box">小张</div>
      <div class="box4 box">小孙</div>
      <div class="box5 box">小刘</div>
      <div class="box6 box">小李</div>
    </div>
    <div class="tc mb24">
      <el-button type="primary" class="btn" @click="handleClick"> 开始抽奖 </el-button>
    </div>
    <div class="text" />
  </div>
</template>

<script setup lang="ts">
const handleClick = () => {
    var num = 0
    const timer = setInterval(() => {
        num++
        // 1-6随机整数
        const random = Math.floor(Math.random() * 6 + 1)
        const selectBox = document.querySelector(`.box${random}`) as HTMLDivElement
        const allBox = document.querySelectorAll('.box')
        const text = document.querySelector('.text') as HTMLDivElement
        text.innerHTML = ''
        for (let i = 0; i < allBox.length; i++) {
            (allBox[i] as HTMLDivElement).style.backgroundColor = ''
        }
        selectBox.style.backgroundColor = 'red'
        if (num >= 50) {
            text.innerHTML = `恭喜${selectBox.innerHTML}中奖`
            clearInterval(timer)
        }
    }, 50)
}
</script>

<style lang="less" scoped>
.random {
  .container {
    width: 600px;
    height: 200px;
    display: flex;
    flex-wrap: wrap;
    border: 1px solid black;
    margin: 30px auto;
  }

  .container div {
    width: 200px;
    height: 100px;
    text-align: center;
    line-height: 100px;
  }

  .btn {
    margin: auto auto;
  }

  .text {
    text-align: center;
    font-size: 24px;
    font-weight: bold;
  }
}
</style>
